<!DOCTYPE html>
<html>
<head>
    

    

    



    <meta charset="utf-8">
    
    
    
    <title>有无符号变量注意点 | Sailing | Sailing&#39;s Home</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="cpp">
    <meta name="description" content="1.字符型 种类：char / signed char / unsigned char 注意：尽管类型有三种，但是字符的表现形式只有两种：有符号和无符号。char属于两面派，实际上会表现为上述两种的一种，具体由编译器决定。因此，尽量不要用char  2.有符号和无符号的类型转换 有符号转无符号：如果超出范围，结果是初始">
<meta name="keywords" content="cpp">
<meta property="og:type" content="article">
<meta property="og:title" content="有无符号变量注意点">
<meta property="og:url" content="http://lishangying.github.io/2017/12/30/有无符号变量注意点/index.html">
<meta property="og:site_name" content="Sailing">
<meta property="og:description" content="1.字符型 种类：char / signed char / unsigned char 注意：尽管类型有三种，但是字符的表现形式只有两种：有符号和无符号。char属于两面派，实际上会表现为上述两种的一种，具体由编译器决定。因此，尽量不要用char  2.有符号和无符号的类型转换 有符号转无符号：如果超出范围，结果是初始值对无符号类型表示数值总数取模后的余数 无符号转有符号：如果超出范围，结果是未定">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2018-01-01T15:46:07.034Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="有无符号变量注意点">
<meta name="twitter:description" content="1.字符型 种类：char / signed char / unsigned char 注意：尽管类型有三种，但是字符的表现形式只有两种：有符号和无符号。char属于两面派，实际上会表现为上述两种的一种，具体由编译器决定。因此，尽量不要用char  2.有符号和无符号的类型转换 有符号转无符号：如果超出范围，结果是初始值对无符号类型表示数值总数取模后的余数 无符号转有符号：如果超出范围，结果是未定">
    
        <link rel="alternate" type="application/atom+xml" title="Sailing" href="/atom.xml">
    
    <link rel="shortcut icon" href="/favicon.ico">
    <link rel="stylesheet" href="//unpkg.com/hexo-theme-material-indigo@latest/css/style.css">
    <script>window.lazyScripts=[]</script>

    <!-- custom head -->
    

</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/img/brand.jpg)">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">Sailing</h5>
          <a href="mailto:18810691321@163.com" title="18810691321@163.com" class="mail">18810691321@163.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                Archives
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                Tags
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/categories"  >
                <i class="icon icon-lg icon-th-list"></i>
                Categories
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="http://lishangying.github.io" target="_blank" >
                <i class="icon icon-lg icon-github"></i>
                Github
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">有无符号变量注意点</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">有无符号变量注意点</h1>
        <h5 class="subtitle">
            
                <time datetime="2017-12-30T06:50:28.000Z" itemprop="datePublished" class="page-time">
  2017-12-30
</time>


            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#1-字符型"><span class="post-toc-number">1.</span> <span class="post-toc-text">1.字符型</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#2-有符号和无符号的类型转换"><span class="post-toc-number">2.</span> <span class="post-toc-text">2.有符号和无符号的类型转换</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#3-有符号和无符号的运算"><span class="post-toc-number">3.</span> <span class="post-toc-text">3.有符号和无符号的运算</span></a></li></ol>
        </nav>
    </aside>
    
<article id="post-有无符号变量注意点"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">有无符号变量注意点</h1>
        <div class="post-meta">
            <time class="post-time" title="2017-12-30 14:50:28" datetime="2017-12-30T06:50:28.000Z"  itemprop="datePublished">2017-12-30</time>

            


            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <h4 id="1-字符型"><a href="#1-字符型" class="headerlink" title="1.字符型"></a>1.字符型</h4><ol>
<li><strong>种类</strong>：char / signed char / unsigned char</li>
<li><strong>注意</strong>：尽管类型有三种，但是字符的表现形式只有两种：有符号和无符号。char属于两面派，实际上会表现为上述两种的一种，具体由编译器决定。<strong>因此，尽量不要用char</strong></li>
</ol>
<h4 id="2-有符号和无符号的类型转换"><a href="#2-有符号和无符号的类型转换" class="headerlink" title="2.有符号和无符号的类型转换"></a>2.有符号和无符号的类型转换</h4><ol>
<li><strong>有符号转无符号</strong>：如果超出范围，结果是初始值对无符号类型表示数值总数取模后的余数</li>
<li><strong>无符号转有符号</strong>：如果超出范围，结果是<strong>未定义的</strong></li>
</ol>
<pre><code class="cpp"><span class="keyword">bool</span> b = <span class="number">42</span>;        <span class="comment">//b的值为真</span>
<span class="keyword">int</span> i = b;            <span class="comment">//i的值为1</span>

i = <span class="number">3.14</span>;            <span class="comment">//i的值为3</span>
<span class="keyword">double</span> pi = i;        <span class="comment">//pi的值为3.0</span>

usigned <span class="keyword">char</span> c1 = <span class="number">-1</span>;<span class="comment">//假设char占8bit，c1的值为255</span>
<span class="keyword">signed</span> <span class="keyword">char</span> c2 = <span class="number">256</span>;<span class="comment">//假设char占8bit，c2的值为未定义的</span>
</code></pre>
<h4 id="3-有符号和无符号的运算"><a href="#3-有符号和无符号的运算" class="headerlink" title="3.有符号和无符号的运算"></a>3.有符号和无符号的运算</h4><ol>
<li><strong>同种类型的有无符号算术运算</strong>：<strong><em>有符号{运算}无符号 =&gt;无符号</em></strong><br>这里指的是同种类型的，因为同种类型的有无符号的两个数，无符号的数值表示的值更大</li>
</ol>
<pre><code class="cpp"><span class="keyword">int</span> u1 = <span class="number">10</span>;
<span class="keyword">unsigned</span> <span class="keyword">int</span> u2 = <span class="number">-42</span>;
<span class="built_in">std</span>::<span class="built_in">cout</span>&lt;&lt; u1 + u1 &lt;&lt; <span class="built_in">std</span>::<span class="built_in">endl</span>;<span class="comment">//输出84</span>
<span class="built_in">std</span>::<span class="built_in">cout</span>&lt;&lt; u1 + u2 &lt;&lt; <span class="built_in">std</span>::<span class="built_in">endl</span>;<span class="comment">//如果int是32位，输出4294967264</span>
</code></pre>
<ol>
<li><strong>同中类型的有无符号比较</strong>：<strong><em>特别是循环中常用的比较语句</em></strong></li>
</ol>
<pre><code class="cpp"><span class="comment">//正确的</span>
<span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">10</span>;i &gt;= <span class="number">0</span>; --i){
    <span class="built_in">std</span>::<span class="built_in">cout</span> &lt;&lt; i &lt;&lt; <span class="built_in">std</span>::<span class="built_in">endl</span>;
}
<span class="comment">//错误:变量u永远不会小于0，循环条件一直成立</span>
<span class="keyword">for</span>(usgned <span class="keyword">int</span> u = <span class="number">10</span>;u &gt;= <span class="number">0</span>; --u){
    <span class="built_in">std</span>::<span class="built_in">cout</span> &lt;&lt; i &lt;&lt; <span class="built_in">std</span>::<span class="built_in">endl</span>;
}
</code></pre>
<p><strong>注意</strong>：用于循环条件比较的变量：<br>大于的形式，最好是有符号的；<br>小于的形式，最好是无符号的。</p>

        </div>

        <blockquote class="post-copyright">
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2018-01-01T15:46:07.034Z" itemprop="dateUpdated">2018-01-01 23:46:07</time>
</span><br>


        
    </div>
    <footer>
        <a href="http://lishangying.github.io">
            <img src="/img/avatar.jpg" alt="Sailing">
            Sailing
        </a>
    </footer>
</blockquote>

        


        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/cpp/">cpp</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://lishangying.github.io/2017/12/30/有无符号变量注意点/&title=《有无符号变量注意点》 — Sailing&pic=http://lishangying.github.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://lishangying.github.io/2017/12/30/有无符号变量注意点/&title=《有无符号变量注意点》 — Sailing&source=A blog of Sailing" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://lishangying.github.io/2017/12/30/有无符号变量注意点/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《有无符号变量注意点》 — Sailing&url=http://lishangying.github.io/2017/12/30/有无符号变量注意点/&via=http://lishangying.github.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://lishangying.github.io/2017/12/30/有无符号变量注意点/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2017/12/30/变量初始化注意点/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">变量初始化注意点</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2017/12/19/hexo+github OSChina/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">hexo+github/OSChina</h4>
      </a>
    </div>
  
</nav>



    

</article>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            
                <span><a href="/atom.xml" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            
            <span>博客内容遵循 <a rel="license" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">知识共享 署名 - 非商业性 - 相同方式共享 4.0 国际协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p><span>Sailing &copy; 2015 - 2018</span>
            <span>
                
                Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a>
            </span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://lishangying.github.io/2017/12/30/有无符号变量注意点/&title=《有无符号变量注意点》 — Sailing&pic=http://lishangying.github.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://lishangying.github.io/2017/12/30/有无符号变量注意点/&title=《有无符号变量注意点》 — Sailing&source=A blog of Sailing" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://lishangying.github.io/2017/12/30/有无符号变量注意点/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《有无符号变量注意点》 — Sailing&url=http://lishangying.github.io/2017/12/30/有无符号变量注意点/&via=http://lishangying.github.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://lishangying.github.io/2017/12/30/有无符号变量注意点/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: false };


</script>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/main.min.js"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/search.min.js" async></script>






<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>





</body>
</html>
